home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Languages Suite
/
ProgramD2.iso
/
Borland
/
Borland C++ V5.02
/
STDLIB.PAK
/
MUTEX.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1997-05-06
|
721b
|
41 lines
//
// This is a code snippet illustrating how to use mutexes to
// synchronize resources in a multi-threaded program.
//
#include <stdmutex.h>
//
// An integer shared amongst multiple threads.
//
int I;
//
// A mutex used to synchronize updates to I.
//
RWSTDMutex I_mutex;
//
// Increment I by one. Uses a RWSTDMutex directly.
//
void increment_I ()
{
I_mutex.acquire(); // Lock the mutex.
I++;
I_mutex.release(); // Unlock the mutex.
}
//
// Decrement I by one. Uses a RWSTDGuard.
//
void decrement_I ()
{
RWSTDGuard guard(I_mutex); // Acquire the lock on I_mutex.
--I;
//
// The lock on I is released when destructor is called on guard.
//
}